from django.db import models
from users.models import Usermodel as User

# Create your models here.
# models.py
class BaseModel(models.Model):
    """
    抽象模型类
    """
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        abstract = True  # 抽象模型类  执行迁移时,不会在数据库中创建表

#  文章类型表
class LifestyleType(BaseModel):
    WZ_CLASS_CHOICES = (
        (1, "生活方式"),
        (2, "灵感"),
        (3, "时尚"),
        (4, "文化"),
    )
    wz_class = models.SmallIntegerField(default=1, choices=WZ_CLASS_CHOICES, verbose_name="文章类型")
    class Meta:
        db_table = "lifestyle_type"
        verbose_name = "文章类型"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.get_wz_class_display()

#图片上传路径
def fm_image_upload_to(instance, filename):
    """
    自定义上传图片路径
    :param instance:
    :param filename:
    :return:
    """
    file_path = instance.id + '/' + filename
    return file_path

# 文章标签表
class LifestyleTag(BaseModel):
    tag_name = models.CharField(max_length=100, verbose_name="标签名称")
    blog = models.ForeignKey(to="Lifestyle",null=True, blank=True, on_delete=models.DO_NOTHING, related_name="blogs", verbose_name="文章标签")
    class Meta:
        db_table = "lifestyle_tag"
        verbose_name = "文章标签"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.tag_name

# 文章表
class Lifestyle(BaseModel):
    user = models.ForeignKey(User, on_delete=models.DO_NOTHING, null=True, blank=True, verbose_name='作者')
    title = models.CharField(max_length=100, verbose_name="标题")
    desc = models.TextField(verbose_name="文章描述")
    body = models.TextField(verbose_name="文章内容")
    category = models.ForeignKey(LifestyleType, on_delete=models.DO_NOTHING, null=True, blank=True ,related_name="lifestyle_type",verbose_name='文章类型')
    fm_image = models.ImageField(upload_to=fm_image_upload_to, null=True, blank=True, verbose_name="封面图片")
    view_nums = models.IntegerField(default=0, verbose_name="阅读量")
    thumbsUp = models.IntegerField(default=0, verbose_name="点赞量")
    collection = models.IntegerField(default=0, verbose_name="收藏量")
    hot = models.IntegerField(default=0, verbose_name="热度值")
    STATUS_CHOICES = (
        (1, "已发表"),
        (2, "草稿"),
        (3, "下架"),
    )
    status = models.SmallIntegerField(default=2, choices=STATUS_CHOICES, verbose_name="文章状态")

    class Meta:
        db_table = "lifestyle_article"
        verbose_name = "生活方式文章"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title

# 用户行为表
class UserBehavior(BaseModel):
    user = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="user_action", null=True, blank=True, verbose_name='用户')
    article = models.ForeignKey(Lifestyle, on_delete=models.DO_NOTHING, null=True, blank=True, related_name="action", verbose_name='文章')
    STATUS_CHOICES = (
        (1, "点赞"),
        (2, "收藏"),
        (3, "浏览"),
        (4, "分享"),
        (5, "评论"),
    )
    action = models.SmallIntegerField(default=1, choices=STATUS_CHOICES, verbose_name="行为类型")
    comment = models.TextField(null=True, blank=True, verbose_name="评论内容")


class Editor_Recommend(BaseModel):
    blog = models.ForeignKey(Lifestyle, on_delete=models.DO_NOTHING, related_name="editor", verbose_name="推荐博客")
    # 是否推荐的字段
    REC_CHIOSE = (
        (1, '推荐'),
        (2, '未推荐')
    )
    is_rec = models.SmallIntegerField(default=1, choices=REC_CHIOSE,verbose_name="是否推荐")

    def __str__(self):
        return self.blog.title

    class Meta:
        verbose_name = "编辑推荐"